#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   1.csv.add_old_api_result.py
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/8/22 15:23   qiufengfeng      
'''
import pandas as pd
import requests
import json
import ast

from price_extra import *


def orochi_api(text):
    url = "https://qa.amh-group.com/panamera/quark/quark"
    header = {
        'Content-Type': "application/json"
    }
    post_data = {"text":  text}
    req = requests.post(url, data=json.dumps(post_data), headers=header)

    driver_target_price = 0
    shipper_targe_price = 0
    if req.json():
        if 'data' in req.json():
            if req.json()['data']["shipperPriceConfidence"] >= 0.9 and req.json()['data']["shipperPriceType"] == 1:
                shipper_targe_price = req.json()['data']["shipperTargetPrice"]

            if req.json()['data']["driverPriceConfidence"] >= 0.9 and req.json()['data']["driverPriceType"] == 1:
                driver_target_price = req.json()['data']["driverTargetPrice"]

    return shipper_targe_price,driver_target_price


def make_biaozhu_target_price(item):
    text = item["text"]
    label = item['label']
    if type(label) == str:
        label = ast.literal_eval(label)

    driver_target_price_texts = []
    shipper_target_price_texts = []
    for (start,end,price_type) in label:
        price_text = text[start:end]
        if price_type == "司机意向价":
            driver_target_price_texts.append(price_text)
        elif price_type == "货主意向价":
            shipper_target_price_texts.append(price_text)

    driver_target_prices = []
    shipper_target_prices = []
    for t in driver_target_price_texts:
        conver_p = price_extract(t)
        for p in conver_p:
            if p['price_type'] is not None and  p['price_type'] not in ["到车费",'运费','总价',""]:
                continue

            driver_target_prices.extend(p['price_nums'])

    for t in shipper_target_price_texts:
        conver_p = price_extract(t)
        for p in conver_p:
            if p['price_type'] is not None and  p['price_type'] not in ["到车费",'运费','总价',""]:
                continue
            shipper_target_prices.extend(p['price_nums'])
    driver_target_prices = [p for p in driver_target_prices if p> 100 and p% 5 == 0]
    shipper_target_prices = [p for p in shipper_target_prices if p > 100 and p % 5 == 0]
    if driver_target_prices:
        driver_target_prices = max(driver_target_prices)
    else:
        driver_target_prices = 0

    if shipper_target_prices:
        shipper_target_prices = min(shipper_target_prices)
    else:
        shipper_target_prices = 0
    return driver_target_prices,shipper_target_prices













def prepare_data(input_file,output_file):
    df = pd.read_csv(input_file)
    df['biaozhu_driver_price'], df["biaozhu_shipper_price"] = zip(*df.apply(make_biaozhu_target_price, axis=1))
    df["old_shipper_price"],df['old_driver_price'] = zip(*df["text"].apply(orochi_api))


    df_save  = df[["id","text","biaozhu_driver_price","biaozhu_shipper_price","old_driver_price","old_shipper_price"]]
    df_save.to_csv(output_file)





if __name__ == '__main__':
    input_file = r"model_result_check/0.csv"
    output_file = r'model_result_check/1.csv'
    prepare_data(input_file,output_file)
